package com.googlecode.maven.plugin.appassembler.booter.classworlds;

import com.googlecode.maven.plugin.appassembler.App;
import com.googlecode.maven.plugin.appassembler.ResourceUtils;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import org.apache.maven.plugin.MojoFailureException;

/**
 * Generates files for the classworlds booter.
 */
public final class ClassworldsBooter
    {
    /**
     * Booter class name.
     */
    private static final String BOOTER_CLASS = "org.codehaus.plexus.classworlds.launcher.Launcher";
    /**
     * Resource prefix of template files.
     */
    private static final String RESOURCE_PREFIX = "classpath:/com/googlecode/maven/plugin/appassembler/booter/classworlds/";

    /**
     * Constructs a new ClassworldsBooter object.
     */
    private ClassworldsBooter()
        {
        super();
        }

    /**
     * Generate files for the classworlds booter.
     *
     * @param configuration Classworlds booter configuration.
     * @param app Application.
     * @param targetDirectory Target directory.
     * @throws MojoFailureException If this operation failed.
     */
    public static void apply(ClassworldsBooterConfiguration configuration,
            App app, File targetDirectory)
            throws MojoFailureException
        {
        app.setBooterClass(BOOTER_CLASS);
        app.getJavaParameters().add("-Dclassworlds.conf=conf/classworlds.conf");

        if ((app.getBootArtifacts() == null) || app.getBootArtifacts().isEmpty())
            {
            throw new MojoFailureException("No boot artifacts specified.");
            }

        Map<String, Object> vars = new HashMap<String, Object>();
        vars.put("app", app);
        vars.put("configuration", configuration);

        File targetFile = new File(targetDirectory, "conf/classworlds.conf");
        ResourceUtils.copyTemplateResource(
                RESOURCE_PREFIX + "conf/classworlds.conf.ftl", targetFile, vars);
        }
    }
